package DatagramPlusPlus;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Rafael
 */
public class DatagramPlusPlusServerSocket extends SocketImpl {

    private DatagramPlusPlusSocket socket;
    private boolean close = false;
    private int tamanhoJanela = 8;

    ServerSocket teste;

    // <editor-fold defaultstate="collapsed" desc="Construtores">
    /**
     * Cria um DatagramPlusPlusServerSocket e coloca a escuta numa porta
     * @param port
     * @throws SocketException
     */
    public DatagramPlusPlusServerSocket(int port) throws SocketException  {
        socket = new DatagramPlusPlusSocket(port);
        initServer();
    }

    private void initServer() {
        
    }
    // </editor-fold>

    public DatagramPlusPlusSocket accept() throws SocketException  {
        
        try{
            // Recebe pedido de ligacao
            DatagramPlusPlusPacket pack = new DatagramPlusPlusPacket(new DatagramType(DatagramType.INIT));
            socket.getUDPSocket().receive(pack.getUDPPacket());
                
            // Confirma ligacao
            DatagramPlusPlusPacket reply = new DatagramPlusPlusPacket(new DatagramType(DatagramType.INIT_REPLY));
            socket.getUDPSocket().send(reply.getUDPPacket());
                
            // Espera ACK
            DatagramPlusPlusPacket ack = new DatagramPlusPlusPacket(new DatagramType(DatagramType.INIT));
            socket.getUDPSocket().receive(ack.getUDPPacket());

            DatagramPlusPlusSocket r;
            r = new DatagramPlusPlusSocket(pack.getAddress(), pack.getPort());
            return r;

        }catch (IOException ex) {
                SocketException exc = new SocketException();
                System.err.println("Nao foi possivel aceitar ligacao.");
                throw exc;
        }
        
    }
    /**
     * Closes this socket.
     */
    public void close() {
        socket.close();
        close = true;
    }
    /**
     * Binds this socket to the specified local IP address and port number.
     * @param endpoint
     */
//    public void bind(SocketAddress endpoint) throws SocketException {
//        socket.bind(endpoint);
//    }
    /**
     * Returns the local address of this server socket.
     * @return
     */
    @Override
    public InetAddress	getInetAddress()  {
        return null;

    }
    /**
     * Returns the port on which this socket is listening.
     * @return
     */
    @Override
    public int getLocalPort()  {
        return socket.getLocalPort();
    }
    /**
     * Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
     * @return
     */
    public SocketAddress getLocalSocketAddress()   {
        return socket.getUDPSocket().getLocalSocketAddress();
    }
    /**
     * Gets the value of the SO_RCVBUF option for this ServerSocket, that is the
     * proposed buffer size that will be used for Sockets accepted from this ServerSocket.
     * @return
     * /
    public int getReceiveBufferSize() throws SocketException  {
        return socket.getReceiveBufferSize();
    }
    /**
     * Tests if SO_REUSEADDR is enabled.
     * @return
     * /
    public boolean getReuseAddress() throws SocketException   {
        return socket.getReuseAddress();
    }
    /**
     * Retrive setting for SO_TIMEOUT.
     * @return
     */
    public int getSoTimeout() throws SocketException  {
        SocketException ex = new SocketException("Nao implementado.");
        throw ex;
    }
    /**
     * Returns the binding state of the ServerSocket.
     * @return
     */
    public boolean isBound()   {
        if(close)
            return false;
        return true;
    }
    /**
     * Returns the closed state of the ServerSocket.
     * @return
     */
    public boolean isClosed()   {
        return close;
    }
    /**
     * Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.
     */
    public void setReceiveBufferSize(int size) throws SocketException  {
        SocketException ex = new SocketException("Nao implementado.");
        throw ex;
    }
    /**
     * Enable/disable the SO_REUSEADDR socket option.
     * @param on
     */
    public void setReuseAddress(boolean on) throws SocketException {
        SocketException ex = new SocketException("Nao implementado.");
        throw ex;
    }
    /**
     * Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
     * @param timeout
     */
    public void setSoTimeout(int timeout) throws SocketException   {
        SocketException ex = new SocketException("Nao implementado.");
        throw ex;
    }

    @Override
    protected void create(boolean stream) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void connect(String host, int port) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void connect(InetAddress address, int port) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void connect(SocketAddress address, int timeout) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void bind(InetAddress host, int port) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void listen(int backlog) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void accept(SocketImpl s) throws IOException {
        accept();
    }

    @Override
    protected InputStream getInputStream() throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected OutputStream getOutputStream() throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected int available() throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void sendUrgentData(int data) throws IOException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setOption(int optID, Object value) throws SocketException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getOption(int optID) throws SocketException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
